#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram


library(tidyverse)

dataset <- diamonds

ui <- fluidPage(
  
  title = "Diamonds Explorer",
  
  plotOutput('plot'),
  
  hr(),
  
  fluidRow(
    column(5,
           h4('设置参数'),
           sliderInput(
             "shoujia", "售价",
             min = 30, max = 100,
             value = 60,
             step = 5, round = 0),
           br(),
           sliderInput(
             "chengben", "成本",
             min = 30, max = 65,
             value = 35,
             step = 5, round = 0
           )
    )
  ),
  fluidRow(
    column(5,  tableOutput("table"))
  )
)



# Define server logic required to draw a histogram




server <- function(input, output) {
  #== krup
  slider3.1 <- reactive({
    df3.1 <- readxl::read_excel(df.fpath, sheet = "c3.1") 
    gu.ding.fei.yong <- 60
    df3.1.tmp <- df3.1 %>% 
      mutate(成本 = gu.ding.fei.yong + 产量 * input$chengben/10000) %>%
      mutate(收入 = input$shoujia/10000 * 产量) %>% 
      mutate(利润 = 收入 - 成本)
    df3.1.tmp
  })
  
  output$plot <- renderPlot({
    df3.1.tmp <- slider3.1() %>% gather(key = "分类", value = "金额", -产量) 
    #==
    p3.1 <- ggplot(df3.1.tmp, aes(产量, 金额, color = 分类)) +
      geom_line() +
      geom_point() +
      ggtitle("本量利分析表")
    print(p3.1)
  })
  
  output$table <- renderTable({
   slider3.1()
  })
}



# Run the application 
shinyApp(ui = ui, server = server)

